{% extends "base.html" %}

{% block content %}
<!-- 区域设备概览 -->
<div class="section">
    <div class="section-title">🌍 区域设备概览</div>
    <div class="section-content">
        {% if data.region_overview %}
        <div class="stats-cards">
            <div class="stat-card">
                <div class="stat-value">{{ data.region_overview.region_name }}</div>
                <div class="stat-label">分析区域</div>
            </div>
            <div class="stat-card">
                <div class="stat-value">{{ data.region_overview.device_count | format_number }}</div>
                <div class="stat-label">设备总数</div>
            </div>
            <div class="stat-card">
                <div class="stat-value">{{ data.region_overview.project_count | format_number }}</div>
                <div class="stat-label">项目数量</div>
            </div>
            <div class="stat-card">
                <div class="stat-value {{ 'highlight-good' if data.region_overview.efficiency > 0.8 else 'highlight-bad' }}">
                    {{ (data.region_overview.efficiency * 100) | round(1) }}%
                </div>
                <div class="stat-label">区域效率</div>
            </div>
        </div>
        {% endif %}
    </div>
</div>

<!-- 项目分布分析 -->
<div class="section">
    <div class="section-title">📊 项目分布分析</div>
    <div class="section-content">
        {% if data.project_distribution %}
        <table class="data-table">
            <thead>
                <tr>
                    <th>项目名称</th>
                    <th>设备数量</th>
                    <th>工作天数</th>
                    <th>总工作时间</th>
                    <th>平均工作时间</th>
                    <th>项目效率</th>
                </tr>
            </thead>
            <tbody>
                {% for project in data.project_distribution %}
                <tr>
                    <td>{{ project.project_name }}</td>
                    <td>{{ project.device_count | format_number }}</td>
                    <td>{{ project.work_days | format_number }}</td>
                    <td class="highlight-number">{{ project.total_work_time | format_duration }}</td>
                    <td>{{ project.avg_work_time | format_duration }}</td>
                    <td class="{{ 'highlight-good' if project.efficiency > 0.8 else 'highlight-bad' }}">
                        {{ (project.efficiency * 100) | round(1) }}%
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        {% else %}
        <div class="no-data">暂无项目分布数据</div>
        {% endif %}
    </div>
</div>

<!-- 设备类型分析 -->
<div class="section">
    <div class="section-title">🔧 设备类型分析</div>
    <div class="section-content">
        {% if data.device_type_analysis %}
        <table class="data-table">
            <thead>
                <tr>
                    <th>设备类型</th>
                    <th>设备数量</th>
                    <th>占比</th>
                    <th>总工作时间</th>
                    <th>平均工作时间</th>
                    <th>类型效率</th>
                </tr>
            </thead>
            <tbody>
                {% for device_type in data.device_type_analysis %}
                <tr>
                    <td>{{ device_type.type_name }}</td>
                    <td>{{ device_type.count | format_number }}</td>
                    <td>{{ (device_type.percentage * 100) | round(1) }}%</td>
                    <td class="highlight-number">{{ device_type.total_work_time | format_duration }}</td>
                    <td>{{ device_type.avg_work_time | format_duration }}</td>
                    <td class="{{ 'highlight-good' if device_type.efficiency > 0.8 else 'highlight-bad' }}">
                        {{ (device_type.efficiency * 100) | round(1) }}%
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        {% else %}
        <div class="no-data">暂无设备类型数据</div>
        {% endif %}
    </div>
</div>

<!-- 时间段工作分布 -->
<div class="section">
    <div class="section-title">⏰ 时间段工作分布</div>
    <div class="section-content">
        {% if data.time_distribution %}
        <table class="data-table">
            <thead>
                <tr>
                    <th>时间段</th>
                    <th>工作设备数</th>
                    <th>工作时长</th>
                    <th>使用率</th>
                </tr>
            </thead>
            <tbody>
                {% for time_slot in data.time_distribution %}
                <tr>
                    <td>{{ time_slot.time_range }}</td>
                    <td>{{ time_slot.active_devices | format_number }}</td>
                    <td class="highlight-number">{{ time_slot.work_duration | format_duration }}</td>
                    <td class="{{ 'highlight-good' if time_slot.usage_rate > 0.8 else 'highlight-bad' }}">
                        {{ (time_slot.usage_rate * 100) | round(1) }}%
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        {% else %}
        <div class="no-data">暂无时间分布数据</div>
        {% endif %}
    </div>
</div>

<!-- 区域表现排名 -->
<div class="section">
    <div class="section-title">🏆 区域内项目排名</div>
    <div class="section-content">
        {% if data.project_rankings %}
        <div style="margin-bottom: 20px;">
            <h4 style="color: #28a745; margin-bottom: 10px;">⬆️ 效率排名前三项目</h4>
            <table class="data-table">
                <thead>
                    <tr>
                        <th>排名</th>
                        <th>项目名称</th>
                        <th>设备数量</th>
                        <th>总工作时间</th>
                        <th>项目效率</th>
                    </tr>
                </thead>
                <tbody>
                    {% for project in data.project_rankings.top_projects %}
                    <tr>
                        <td>{{ loop.index }}</td>
                        <td>{{ project.project_name }}</td>
                        <td>{{ project.device_count | format_number }}</td>
                        <td class="highlight-good">{{ project.total_work_time | format_duration }}</td>
                        <td class="highlight-good">{{ (project.efficiency * 100) | round(1) }}%</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        {% if data.project_rankings.bottom_projects %}
        <div>
            <h4 style="color: #dc3545; margin-bottom: 10px;">⬇️ 效率排名后三项目</h4>
            <table class="data-table">
                <thead>
                    <tr>
                        <th>排名</th>
                        <th>项目名称</th>
                        <th>设备数量</th>
                        <th>总工作时间</th>
                        <th>项目效率</th>
                    </tr>
                </thead>
                <tbody>
                    {% for project in data.project_rankings.bottom_projects %}
                    <tr>
                        <td>倒数第{{ loop.index }}</td>
                        <td>{{ project.project_name }}</td>
                        <td>{{ project.device_count | format_number }}</td>
                        <td class="highlight-bad">{{ project.total_work_time | format_duration }}</td>
                        <td class="highlight-bad">{{ (project.efficiency * 100) | round(1) }}%</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% endif %}
        {% else %}
        <div class="no-data">暂无排名数据</div>
        {% endif %}
    </div>
</div>

<!-- AI分析内容 -->
<div class="section">
    <div class="section-title">🤖 智能分析结论</div>
    <div class="section-content">
        {% if data.content_sections %}
            {% for section_title, content in data.content_sections.items() %}
            <div style="margin-bottom: 20px;">
                <h4 style="color: #1f4e79; margin-bottom: 12px; border-bottom: 1px solid #e0e0e0; padding-bottom: 8px;">{{ section_title }}</h4>
                <div style="text-align: justify; line-height: 1.8; margin-top: 12px;">
                    {{ content | safe }}
                </div>
            </div>
            {% endfor %}
        {% else %}
        <div class="no-data">AI分析内容生成中...</div>
        {% endif %}
    </div>
</div>

<!-- 区域管理建议 -->
<div class="section">
    <div class="section-title">💡 区域管理建议</div>
    <div class="section-content">
        <div class="success">
            <h4>优化建议</h4>
            <ul class="report-list">
                {% if data.region_overview and data.region_overview.efficiency < 0.8 %}
                <li><strong>提升区域效率：</strong>当前区域整体效率为 {{ (data.region_overview.efficiency * 100) | round(1) }}%，建议优化设备配置和使用流程。</li>
                {% endif %}
                
                {% if data.project_distribution %}
                <li><strong>项目资源平衡：</strong>在 {{ data.project_distribution | length }} 个项目中合理分配设备资源，避免资源闲置。</li>
                {% endif %}
                
                {% if data.device_type_analysis %}
                <li><strong>设备类型优化：</strong>根据不同设备类型的效率表现，调整设备配置策略。</li>
                {% endif %}
                
                <li><strong>跨项目协调：</strong>建立项目间设备共享机制，提高整体资源利用率。</li>
                <li><strong>定期评估：</strong>每月对区域内各项目进行效率评估，及时调整管理策略。</li>
            </ul>
        </div>
    </div>
</div>
{% endblock %}